package cn.wolfcode.web.controller;

import cn.wolfcode.enums.ConsumptionReportEnum;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author JMJ
 * @date 2021/1/17 16:44
 */
@Controller
@RequestMapping("/consumptionReport")
public class consumptionReportController {
    @Autowired
    private IConsumptionReportService consumptionReportService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String reportList(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {
        model.addAttribute("pageInfo", consumptionReportService.selectConsumptionReport(qo));
        model.addAttribute("enums", ConsumptionReportEnum.values());
        model.addAttribute("business", businessService.listAll());
        return "/consumptionReport/list";
    }

    @RequestMapping("/listBar")
    public String reportBar(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {
        qo.setPageSize(0);
        List<HashMap> list = consumptionReportService.selectConsumptionReport(qo).getList();
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> numberList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();
        for (HashMap m : list) {
            xList.add(m.get("groupType"));
            numberList.add(m.get("number"));
            totalAmountList.add(m.get("totalAmount"));
            payAmountList.add(m.get("payAmount"));
            discountAmountList.add(m.get("discountAmount"));
        }
        model.addAttribute("groupType", JSON.toJSONString(xList));
        model.addAttribute("number", JSON.toJSONString(numberList));
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmount", JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmount", JSON.toJSONString(discountAmountList));
        return "/consumptionReport/listBar";
    }
}
